<template>
  <div v-if="isExternal" class="svg-external-icon svg-icon" :style="styleExternalIcon" :class="className">
    {{ icon }} -- {{ className }} -- {{ isExternal }}
  </div>
  <svg v-else class="svg-icon" :class="className" aria-hidden="true">
      <use :xlink:href="iconName"></use>
  </svg>
</template>

<script>
import { isExternal as external } from "../../utils/validate";
export default {
  props: {
    icon: {
      type: String,
      // 必传
      required: true,
    },
    className: {
      type: String,
      default: "",
    },
  },
  data() {
    return {};
  },
  computed: {
    // 验证是否为外部svg图标
    isExternal() {
      return external(this.icon);
    },
    // 加载外部资源svg图标
    styleExternalIcon() {
      return {
        mask: `url(${this.icon}) no-repeat 50% 50%`,
        "-webkit-mask": `url(${this.icon}) no-repeat 50% 50%`,
      };
    },
    // 加载内部svg图标
    iconName(){
        return `#icon-${this.icon}`
    }
  },
  mounted() {},
  methods: {},
};
</script>

<style scoped lang='scss'>
.svg-icon {
  width: 1em;
  height: 1em;
  vertical-align: -0.15em;
  fill: currentColor;
  overflow: hidden;
}

.svg-external-icon {
  background-color: currentColor;
  mask-size: cover !important;
  display: inline-block;
}
</style>
